/**
 * @Author ZhangCuirong
 * @Date 2025/8/14 9:40
 * @description:
 */
public class Solution8 {
    public int myAtoi(String s) {
        if (s == null || s.isEmpty()) {
            return 0;
        }
        s = s.trim();
        if (s.isEmpty()) {
            return 0;
        }
        boolean isNegative = false;
        long sum = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i == 0 && s.charAt(i) == '-') {
                isNegative = true;
            } else if (i == 0 && s.charAt(i) == '+') {
                continue;

            } else if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
                sum = sum * 10L + (s.charAt(i) - '0');
                if (isNegative && -sum <= Integer.MIN_VALUE) {
                    sum = Integer.MIN_VALUE;
                    break;
                } else if (!isNegative && sum >= Integer.MAX_VALUE) {
                    sum = Integer.MAX_VALUE;
                    break;
                }
            } else {
                break;
            }
        }
        return (int) (isNegative ? -sum : sum);
    }

    public static void main(String[] args) {
        Solution8 solution = new Solution8();
        int res = solution.myAtoi("-91283472332");
        System.out.println(res);
    }
}
